<?php
class jckWidgetFormBrowser extends sfWidgetFormInputText {

  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);
    //$this->setAttribute('readonly', 'readonly');
    $this->addOption('browser_profil');
    $this->addOption('browser_width', 800);
    $this->addOption('browser_height', 600);
    $this->addOption('preview_max_width', 250);
    $this->addOption('preview_max_height', 200);
    $this->addOption('browse_text', 'Browse');
  }

  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    $html = '<img id="preview_{id}"
      src="{value}" style="max-width:{preview_max_width}px;max-height:{preview_max_height}px" />
    <br/>';
    $html .= parent::render($name, $value, $attributes, $errors);
    $html .= '<input type="button" id="browse_{id}" value="{browse_text}"/>
    <script type="text/javascript">
    $(function(){
      $("#browse_{id}").click(function(){
        window.open("'.url_for('jckbrowser_browse').'?jckBrowseField={id}&profil='.$this->getOption('browser_profil').'","win{id}","width={browser_width}, height={browser_height}");
      });
      $("#{id}").bind("change",function(){
       $("#preview_{id}").attr("src", $(this).val());
      });
    });
    </script>
    ';

    // return $html.$js;

     $template_vars = array(
         '{id}'                 =>  $this->generateId($name),
         '{value}'              =>  $value,
         '{preview_max_width}'  =>  $this->getOption('preview_max_width'),
         '{preview_max_height}' =>  $this->getOption('preview_max_height'),
         '{browser_width}'      =>  $this->getOption('browser_width'),
         '{browser_height}'     =>  $this->getOption('browser_height'),
         '{browse_text}'     =>  $this->getOption('browse_text')

    );

    return strtr($html, $template_vars);

  }

  public function  getJavaScripts() {
    return array(
      sfConfig::get('app_jckbrowser_jquery_path')
    );
  }
}
?>